/* shttpd - generic file descriptor operations
   Copyright (C) 2018 Ariadne Devos

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include "fd.h"
#include <sHT/bugs.h>
#include <sHT/compiler.h>
#include <sHT/test.h>

#include <errno.h>
#include <unistd.h>

static const struct sHT_bug_option sHT_close_bug =
  { .code = 0, .msg = "not a fd" };

void
sHT_close(int fd)
{
	/* close must not be retried on interrupt on:
	   Linux, FreeBSD, OpenBSD (man pages),
	   don't know about NetBSD,
	   I presume the Hurd. */
	int ret = close(fd);
	if (!sHT_nonzero_p(ret))
		return;
	int err = errno;
	if (sHT_eq(err, EBADFD))
		sHT_bug(&sHT_close_bug, err);
	return;
}
